package com.san.metadata.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.san.common.utils.CTConversionUtils;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.metadata.controller.TechnicalMetadataController;
import com.san.metadata.dao.TechnicalMetadataDao;
import com.san.metadata.dao.TechnicalMetadataManager;
import com.san.metadata.domain.ColumnDTO;
import com.san.metadata.domain.DataSourceDO;
import com.san.metadata.domain.TableDTO;
import com.san.metadata.domain.TechnicalColumnsDO;
import com.san.metadata.domain.TechnicalTableDO;
import com.san.metadata.service.TechnicalMetadataService;
import com.san.metadata.vo.TechnicalColumnsVO;

/**
 * 技术元数据服务实现类
 * 
 * @author hulp
 *
 */
@Service
public class TechnicalMetadataServiceImpl implements TechnicalMetadataService {

	private static final Logger logger = LoggerFactory.getLogger(TechnicalMetadataController.class);

	@Autowired
	private TechnicalMetadataDao technicalMetadataMapper;
	@Autowired
	private TechnicalMetadataManager technicalMetadataManager;

	@Override
	public PageUtils list(Query query) throws Exception {
		List<TechnicalTableDO> list = technicalMetadataMapper.list(query);
		int total = technicalMetadataMapper.count(query);
		PageUtils pageUtils = new PageUtils(list, total);
		return pageUtils;
	}

	@Override
	public List<TechnicalTableDO> allTechnicalTable(Query query) throws Exception {
		return technicalMetadataMapper.allTechnicalTable(query);
	}

	@Override
	public List<TableDTO> listTechnicalTable() throws Exception {
		return technicalMetadataMapper.listTable();
	}

	@Override
	public List<ColumnDTO> listTechnicalColumns(Long tableId) throws Exception {
		return technicalMetadataMapper.listColumns(tableId);
	}

	@Override
	public int save(TechnicalTableDO technicalTableDO) throws Exception {
		return technicalMetadataMapper.save(technicalTableDO);
	}

	@Override
	public int doEdit(TechnicalTableDO technicalTableDO) throws Exception {
		return technicalMetadataMapper.doEdit(technicalTableDO);
	}

	@Override
	public int remove(Long tableId) throws Exception {
		return technicalMetadataMapper.remove(tableId);
	}

	@Override
	public int batchRemove(Long[] tableIds) throws Exception {
		return technicalMetadataMapper.batchRemove(tableIds);
	}

	@Override
	public TechnicalTableDO getById(Long tableId) throws Exception {
		return technicalMetadataMapper.getById(tableId);
	}

	@Override
	public PageUtils listSourceTable(DataSourceDO dataSourceDO) throws Exception {
		List<Map<String, Object>> list = technicalMetadataManager.listSourceTable(dataSourceDO);
		PageUtils pageUtils = new PageUtils(list, list.size());
		return pageUtils;
	}

	@Override
	public PageUtils listColumns(DataSourceDO dataSourceDO, TechnicalTableDO technicalTableDO) throws Exception {
		List<Map<String, Object>> list = technicalMetadataManager.listColumns(dataSourceDO, technicalTableDO);
		PageUtils pageUtils = new PageUtils(list, list.size());
		return pageUtils;
	}

	@Override
	public int saveColumn(TechnicalColumnsVO technicalColumnsVO) throws Exception {
		String[] columnType = CTConversionUtils
				.O2M(technicalColumnsVO.getColumnType(), technicalColumnsVO.getColumnLength(),
						technicalColumnsVO.getColumnPrecision(), technicalColumnsVO.getColumnScale())
				.split("\\(");
		technicalColumnsVO.setColumnType(columnType[0]);
		return technicalMetadataMapper.saveColumn(technicalColumnsVO);
	}

	@Override
	@Transactional
	public boolean saveTable(int sourceType,Long tableId, DataSourceDO targetDataSourceDO,
			List<TechnicalColumnsVO> technicalColumnsVOList) {
		TechnicalTableDO technicalTableDO = null;
		technicalTableDO = technicalMetadataMapper.getById(tableId);
		boolean flag = technicalMetadataManager.saveTable(sourceType,technicalTableDO, targetDataSourceDO, technicalColumnsVOList);
		if (flag) {
			for (TechnicalColumnsVO i : technicalColumnsVOList) {
				String[] columnType = CTConversionUtils
						.O2M(i.getColumnType(), i.getColumnLength(), i.getColumnPrecision(), i.getColumnScale())
						.split("\\(");
				i.setColumnType(columnType[0]);
				i.setColumnName(i.getColumnName().toUpperCase());
				technicalMetadataMapper.saveColumn(i);
			}
			technicalTableDO.setCreateFlag((long) 0);
			try {
				technicalMetadataMapper.doEdit(technicalTableDO);
			} catch (Exception e) {
				logger.error("更新技术元数据状态失败", e);
			}
		}
		return flag;
	}
	

	@Override
	public List<TechnicalColumnsDO> allTechnicalColumns(Long tableId) throws Exception {
		return technicalMetadataMapper.allTechnicalColumns(tableId);
	}

	@Override
	public int batchColumnsUpdate(List<TechnicalColumnsVO> technicalColumnsVOList) {
		return technicalMetadataMapper.batchColumnsUpdate(technicalColumnsVOList);
	}


}
